<script lang="ts" setup>
import { QForm, QTable, type FormItemRows, type TableColums } from '@meetjs/element-plus-kit'
import { delInterfaceInfo, getInterfaceList } from '@/api/interface/sys/index'
import { warnBox } from '@/utils/utils'
import EditDialog from './components/EditDialog.vue'
import type { InterfaceList, QueryParams } from '@/api/interface/sys/type'

// form
const formValue = ref<QueryParams>({
	businessSystemName: '',
	businessSystemCode: ''
})
const formOptions = ref<FormItemRows[]>([
	{
		type: 'input',
		label: '业务系统名称',
		prop: 'businessSystemName'
	},
	{
		type: 'input',
		label: '业务系统名称',
		prop: 'businessSystemCode'
	}
])

// table
const loading = ref(false)
const page = ref({
	currentPage: 1,
	pageSize: 10,
	total: 0
})
const tableData = ref<InterfaceList[]>([])
const columns = ref<TableColums[]>([
	{
		type: 'index',
		label: '序号',
		align: 'center',
		width: 60
	},
	{
		label: '业务系统名称',
		prop: 'businessSystemName'
	},
	{
		label: '业务系统代码',
		prop: 'businessSystemCode'
	},
	{
		label: '系统类型',
		prop: 'systemType'
	},
	{
		label: '基础URL',
		prop: 'baseUrl'
	},
	{
		label: '联系人',
		prop: 'contactPerson'
	},
	{
		label: '联系电话',
		prop: 'contactPhone'
	},
	{
		label: '联系邮箱',
		prop: 'contactEmail'
	},
	{
		label: '注册时间',
		prop: 'registrationTime'
	},
	{
		prop: 'action',
		fixed: 'right',
		label: '操作',
		width: 160,
		align: 'center'
	}
])
// dialog
const editDialogRef = useTemplateRef('editDialogRef')

const getTableData = async () => {
	try {
		loading.value = true
		const res = await getInterfaceList({
			pageNum: page.value.currentPage,
			pageSize: page.value.pageSize,
			...formValue.value
		})
		tableData.value = res.data.rows
		page.value.total = res.data.total
	} finally {
		loading.value = false
	}
}

const handelAdd = () => {
	editDialogRef.value?.init('add')
}

const handelEdit = (row: InterfaceList) => {
	editDialogRef.value?.init('edit', row)
}
const handelDelete = async (rowId: string) => {
	await warnBox()
	await delInterfaceInfo(rowId)
	ElMessage.success('删除成功')
	getTableData()
}

onMounted(() => {
	getTableData()
})
</script>
<template>
	<div class="table-page-box">
		<div class="filter-box">
			<QForm
				inline
				:model="formValue"
				:form-options="formOptions"
				:buttons="['search', 'reset']"
				@search="getTableData"
				@reset="getTableData"
			/>
		</div>
		<div class="table-box">
			<QTable
				:data="tableData"
				:columns="columns"
				:page="page"
				:loading="loading"
				style="width: 100%"
				@pageChange="getTableData"
			>
				<template #headerLeft>
					<el-button
						type="primary"
						@click="handelAdd"
					>
						新增
					</el-button>
				</template>
				<template #action="{ row }">
					<el-button
						type="primary"
						link
						@click="handelEdit(row)"
					>
						修改
					</el-button>
					<el-button
						type="danger"
						link
						@click="handelDelete(row.id)"
					>
						删除
					</el-button>
				</template>
			</QTable>
		</div>
		<EditDialog
			ref="editDialogRef"
			@refresh="getTableData"
		/>
	</div>
</template>
